﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;

using System.Windows.Media.Imaging;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.IO;

using System.IO.IsolatedStorage;
using System.Xml.Serialization;
using Motif.Serialization;

namespace Motif
{
    //public class image 
    //{
    //    public 
    //}
    public class ImageCollection
    {
        static string filename = "imagecollection";
        [DataMember]
        public Dictionary<int, string> AlbumCollection { get; set; }
        [DataMember]
        public Dictionary<int, int> PicturesCollection { get; set; }

        public static int getTotalImagesCountInMediaLibrary() 
        {
            var ml = new Microsoft.Xna.Framework.Media.MediaLibrary();

            return ml.Pictures.Count;
        }
        public ImageCollection()
        {
            AlbumCollection = new Dictionary<int, string>();
            PicturesCollection = new Dictionary<int, int>();
        }

        public ImageCollection(Dictionary<int, string> ac, Dictionary<int, int> ic)
        {
            AlbumCollection = ac;
            PicturesCollection = ic;
        }

        public void Save()
        {
            using (IsolatedStorageFile myStore = IsolatedStorageFile.GetUserStoreForApplication())
            {
                if (myStore.FileExists(filename))
                {
                    myStore.DeleteFile(filename);
                }
                IsolatedStorageFileStream myFileStream = myStore.CreateFile(filename);
                BinarySerializationHelper.Serialize(myFileStream, this);
                myFileStream.Close();

            }

        }

        public static ImageCollection Load()
        {
            var myStore = IsolatedStorageFile.GetUserStoreForApplication();
            if (myStore.FileExists(filename))
            {
                IsolatedStorageFileStream myFileStream = myStore.OpenFile(filename, FileMode.Open);
                try
                {
                    // deserialization
                    var sampleData = BinarySerializationHelper.Deserialize(myFileStream, typeof(ImageCollection));

                    return (ImageCollection)sampleData;
                }
                finally
                {
                    myFileStream.Close();
                }
            }
            else return new ImageCollection();
        }
    }
}
